package org.fone.utils.task;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.models.auth.In;
import org.fone.service.TableService;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * 用于解析json
 */
public class TaskCreator {
    private Map<Integer,Task> tasks = new HashMap<>();;
    private Map<Integer,List<Integer>> links = new HashMap<>();;
    public TaskCreator(JSONObject params){
        //加载节点
        params.getJSONArray("nodes").forEach(obj->{
            JSONObject json = (JSONObject) obj;
            Task task = toTask(json);
            tasks.put(task.task_id,task);
        });
        //加载线
        params.getJSONObject("links").forEach((fromStr,obj)->
                    links.put(Integer.parseInt(fromStr),((JSONArray) obj).toList(Integer.class)));
//        links=JSONUtil.toBean(params.getJSONObject("links"),Map.class);
        System.out.println(tasks);
        System.out.println(links);
    }
    public Task toTask(JSONObject json){
        String type = json.getStr("type");
        if (ObjectUtils.isEmpty(type)) return null;
        switch (type){
            case "selector":
                return json.toBean(Selector.class);
            case "merger":
                return json.toBean(Merger.class);
            case "filter":
                return json.toBean(Filter.class);
            case "senior":
                return json.toBean(Senior.class);
            case "end":
                System.out.println("--------------------------------------------");
                System.out.println("json = " + json);
                System.out.println("End = " + json.toBean(End.class));
                return json.toBean(End.class);
            default:return null;
        }
    }
    public TaskRunner getTaskRunner(TableService tableService){
        return new TaskRunner(tasks,links,tableService);
    }

}
